﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using V5_DBUtility;
using System.Data.SqlClient;
using V5_Model;

using System.Data.OleDb;
namespace V5_DAL.OleDb {
    public class DALArticle  {

        #region  BaseMethod
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return DbHelperOleDb.GetMaxID("ID", "K_Article");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Article");
            strSql.Append(" where ID=" + ID);
            return DbHelperOleDb.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelArticle model) {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null) {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.ChannelID != null) {
                strSql1.Append("ChannelID,");
                strSql2.Append("" + model.ChannelID + ",");
            }
            if (model.Title != null) {
                strSql1.Append("Title,");
                strSql2.Append("'" + model.Title + "',");
            }
            if (model.KeyWords != null) {
                strSql1.Append("KeyWords,");
                strSql2.Append("'" + model.KeyWords + "',");
            }
            if (model.ClassID != null) {
                strSql1.Append("ClassID,");
                strSql2.Append("" + model.ClassID + ",");
            }
            if (model.CopyFrom != null) {
                strSql1.Append("CopyFrom,");
                strSql2.Append("'" + model.CopyFrom + "',");
            }
            if (model.Author != null) {
                strSql1.Append("Author,");
                strSql2.Append("'" + model.Author + "',");
            }
            if (model.Editor != null) {
                strSql1.Append("Editor,");
                strSql2.Append("'" + model.Editor + "',");
            }
            if (model.Content != null) {
                strSql1.Append("Content,");
                strSql2.Append("'" + model.Content + "',");
            }
            if (model.ShortContent != null) {
                strSql1.Append("ShortContent,");
                strSql2.Append("'" + model.ShortContent + "',");
            }
            if (model.IsUrl != null) {
                strSql1.Append("IsUrl,");
                strSql2.Append("" + model.IsUrl + ",");
            }
            if (model.LinkUrl != null) {
                strSql1.Append("LinkUrl,");
                strSql2.Append("'" + model.LinkUrl + "',");
            }
            if (model.DefaultPicUrl != null) {
                strSql1.Append("DefaultPicUrl,");
                strSql2.Append("'" + model.DefaultPicUrl + "',");
            }
            if (model.Hits != null) {
                strSql1.Append("Hits,");
                strSql2.Append("" + model.Hits + ",");
            }
            if (model.CutomField != null) {
                strSql1.Append("CutomField,");
                strSql2.Append("'" + model.CutomField + "',");
            }
            if (model.Passed != null) {
                strSql1.Append("Passed,");
                strSql2.Append("" + model.Passed + ",");
            }
            if (model.AddDateTime != null) {
                strSql1.Append("AddDateTime,");
                strSql2.Append("'" + model.AddDateTime + "',");
            }
            if (model.UpDateTime != null) {
                strSql1.Append("UpDateTime,");
                strSql2.Append("'" + model.UpDateTime + "',");
            }
            strSql.Append("insert into K_Article(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelArticle model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Article set ");
            if (model.ChannelID != null) {
                strSql.Append("ChannelID=" + model.ChannelID + ",");
            }
            if (model.Title != null) {
                strSql.Append("Title='" + model.Title + "',");
            }
            if (model.KeyWords != null) {
                strSql.Append("KeyWords='" + model.KeyWords + "',");
            }
            if (model.ClassID != null) {
                strSql.Append("ClassID=" + model.ClassID + ",");
            }
            if (model.CopyFrom != null) {
                strSql.Append("CopyFrom='" + model.CopyFrom + "',");
            }
            if (model.Author != null) {
                strSql.Append("Author='" + model.Author + "',");
            }
            if (model.Editor != null) {
                strSql.Append("Editor='" + model.Editor + "',");
            }
            if (model.Content != null) {
                strSql.Append("Content='" + model.Content + "',");
            }
            if (model.ShortContent != null) {
                strSql.Append("ShortContent='" + model.ShortContent + "',");
            }
            if (model.IsUrl != null) {
                strSql.Append("IsUrl=" + model.IsUrl + ",");
            }
            if (model.LinkUrl != null) {
                strSql.Append("LinkUrl='" + model.LinkUrl + "',");
            }
            if (model.DefaultPicUrl != null) {
                strSql.Append("DefaultPicUrl='" + model.DefaultPicUrl + "',");
            }
            if (model.Hits != null) {
                strSql.Append("Hits=" + model.Hits + ",");
            }
            if (model.CutomField != null) {
                strSql.Append("CutomField='" + model.CutomField + "',");
            }
            if (model.Passed != null) {
                strSql.Append("Passed=" + model.Passed + ",");
            }
            if (model.AddDateTime != null) {
                strSql.Append("AddDateTime='" + model.AddDateTime + "',");
            }
            if (model.UpDateTime != null) {
                strSql.Append("UpDateTime='" + model.UpDateTime + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Article ");
            strSql.Append(" where ID=" + ID);
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelArticle GetModel(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from K_Article ");
            strSql.Append(" where ID=" + ID);
            ModelArticle model = new ModelArticle();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ChannelID"] != null && ds.Tables[0].Rows[0]["ChannelID"].ToString() != "") {
                    model.ChannelID = int.Parse(ds.Tables[0].Rows[0]["ChannelID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Title"] != null && ds.Tables[0].Rows[0]["Title"].ToString() != "") {
                    model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["KeyWords"] != null && ds.Tables[0].Rows[0]["KeyWords"].ToString() != "") {
                    model.KeyWords = ds.Tables[0].Rows[0]["KeyWords"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "") {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CopyFrom"] != null && ds.Tables[0].Rows[0]["CopyFrom"].ToString() != "") {
                    model.CopyFrom = ds.Tables[0].Rows[0]["CopyFrom"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Author"] != null && ds.Tables[0].Rows[0]["Author"].ToString() != "") {
                    model.Author = ds.Tables[0].Rows[0]["Author"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Editor"] != null && ds.Tables[0].Rows[0]["Editor"].ToString() != "") {
                    model.Editor = ds.Tables[0].Rows[0]["Editor"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Content"] != null && ds.Tables[0].Rows[0]["Content"].ToString() != "") {
                    model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ShortContent"] != null && ds.Tables[0].Rows[0]["ShortContent"].ToString() != "") {
                    model.ShortContent = ds.Tables[0].Rows[0]["ShortContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsUrl"] != null && ds.Tables[0].Rows[0]["IsUrl"].ToString() != "") {
                    model.IsUrl = int.Parse(ds.Tables[0].Rows[0]["IsUrl"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LinkUrl"] != null && ds.Tables[0].Rows[0]["LinkUrl"].ToString() != "") {
                    model.LinkUrl = ds.Tables[0].Rows[0]["LinkUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DefaultPicUrl"] != null && ds.Tables[0].Rows[0]["DefaultPicUrl"].ToString() != "") {
                    model.DefaultPicUrl = ds.Tables[0].Rows[0]["DefaultPicUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Hits"] != null && ds.Tables[0].Rows[0]["Hits"].ToString() != "") {
                    model.Hits = int.Parse(ds.Tables[0].Rows[0]["Hits"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CutomField"] != null && ds.Tables[0].Rows[0]["CutomField"].ToString() != "") {
                    model.CutomField = ds.Tables[0].Rows[0]["CutomField"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Passed"] != null && ds.Tables[0].Rows[0]["Passed"].ToString() != "") {
                    model.Passed = int.Parse(ds.Tables[0].Rows[0]["Passed"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDateTime"] != null && ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "") {
                    model.AddDateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["AddDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpDateTime"] != null && ds.Tables[0].Rows[0]["UpDateTime"].ToString() != "") {
                    model.UpDateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["UpDateTime"].ToString());
                }
                return model;
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM K_Article ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where  1=1 " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }
        #endregion  Method

        #region K_Article开始
        public bool DeleteList(string IDlist) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Article ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return this.GetList("*", Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return this.GetList("*", PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        public List<ModelArticle> GetModelList(string strWhere) {
            DataSet ds = this.GetList(strWhere);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelArticle>(ds);
            }
            return null;
        }

        public List<ModelArticle> GetModelList(int Top, string strWhere, string filedOrder) {
            DataSet ds = this.GetList(Top, strWhere, filedOrder);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelArticle>(ds);
            }
            return null;
        }

        public List<ModelArticle> GetModelList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            DataSet ds = this.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelArticle>(ds);
            }
            return null;
        }

        public DataSet GetList(string strFiled, string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + strFiled + " ");
            strSql.Append(" FROM K_Article ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int Top, string strWhere, string filedOrder) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) {
                strSql.Append(" Top " + Top.ToString());
            }
            strSql.Append(" " + strFiled + " ");
            strSql.Append(" FROM K_Article ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select Count(*) as ID From K_Article ");
            if (strWhere.Trim() != "") {
                strSqlCount.Append(" where 1=1 " + strWhere);
            }
            RecordCount = Convert.ToInt32("0" + DbHelperOleDb.GetSingle(strSqlCount.ToString()));
            if (PageIndex <= 1) {
                PageIndex = 1;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " " + strFiled + " from K_Article where 1=1 ");
            if (strWhere.Trim() != "") {
                strSql.Append(" " + strWhere);
            }
            if (PageIndex > 1) {
                strSql.Append(" And ID Not In (Select Top " + ((PageIndex - 1) * PageSize) + " ID From K_Article Where 1=1 ");
                if (strWhere.Trim() != "") {
                    strSql.Append(" " + strWhere);
                }
                if (!string.IsNullOrEmpty(strOrder)) {
                    strSql.Append(" Order by " + strOrder);
                }
                strSql.Append(")");
            }
            if (!string.IsNullOrEmpty(strOrder)) {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }
        #endregion K_Article结束
    
    


    }
}
